<?php

namespace App\Console\Commands;
use App\DTOS\Admin\UserMemberDTO;
use App\DTOS\Admin\UserRoleDTO;
use App\DTOS\NeiGouDTO;
use App\Exceptions\ServiceException;
use App\Models\Admin;
use App\Models\Customer;
use App\Models\Game\GameGoods;
use App\Models\Game\PayOrder;
use App\Models\Game\RoleEquip;
use App\Models\Game\Sale;
use App\Models\Game\UserMember;
use App\Models\Game\UserRole;
use App\Models\Game\WhiteList;
use App\Models\Rewards;
use App\Models\Setting;
use App\Services\AnnouncementService;
use App\Services\EsConnector;
use App\Services\Game\DouyinService;
use App\Services\Game\NeigouService;
use App\Services\Game\PayOrderService;
use App\Services\Game\ServerGroupService;
use App\Services\IdentifyService;
use App\Services\RedisService;

use App\Utils\MyLog;
use App\Utils\ResponseUtil;
use Illuminate\Console\Command;
use Illuminate\Support\Facades\Crypt;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Log;
use Illuminate\Support\Str;

use Illuminate\Support\Facades\Cookie;

class Test extends Command
{

    protected $signature = 'command:test {serverId=0} {sleep=20}';

    protected $description = 'Command description';

    private RedisService $RedisService;

    private IdentifyService $identifyService;
    public function __construct(IdentifyService $identifyService,RedisService $RedisService)
    {
        parent::__construct();
        $this->identifyService = $identifyService;
        $this->RedisService = $RedisService;
    }

    // laravel-2023-11-10.log
        // docker exec codeenv bash -c "cd /code/yoyo-xm2-admin-api && php artisan command:test"
    // docker exec phpfpm bash -c "cd /code/yoyo-xm2-admin-api && php artisan command:test"

    public function handle(ServerGroupService $serverGroupService,
                           PayOrderService $payOrderService,DouyinService $douyinService,
                           AnnouncementService $announcementService,
                           NeigouService $NeigouService
    )
    {
        /*$file = fopen('/code/yoyo-xm2-admin-api/9-31.csv', 'r');
        $rowData = [];

        while (($data = fgetcsv($file)) !== false) {
            $rowData[] = $data; // 将每行数据存入数组中
        }
        */

    }


    private function showCharTime($seconds){
        $minute = floor($seconds / 60);
        $second = $seconds % 60;
        echo "{$minute}分{$second}秒";
    }

    /**
     * 更改 es 字段
     */
    private function updateEsField(){

        go(function (){

            $bean = new \EasySwoole\ElasticSearch\RequestBean\Search();
            $bean->setIndex('soul_synthesize');
            $body['track_total_hits'] = true;
            $bean->setBody( ['query'=>['bool'=>['must'=>[['match_all'=>(object)[]]]]],'from'=>0,'size'=>100000] );
            $elasticsearch = app(EsConnector::class)->getInstance();
            $res = json_decode($elasticsearch->client()->search($bean)->getBody(),true);
            //var_dump($res);
            $total = data_get($res,'hits.total.value');

            $returnRes = [];
            $returnRes['code'] = 0;

            if ($total > 0){
                foreach (data_get($res,'hits.hits') as $row){
                    $_id = $row['_id'];
                    $source = $row['_source'];
                    $useClmx = $source['useClmx'] ?? null;
                    if (!$useClmx) {
                        continue;
                    }
                    $useClmx = json_decode($source['useClmx'],true);

                    $bean1 = new \EasySwoole\ElasticSearch\RequestBean\Update();
                    $bean1->setIndex('soul_synthesize');
                    $bean1->setId($_id);
                    $source['use_clmx'] = $useClmx;
                    $source['useClmx'] = null;

                    $bean1->setBody([
                        'doc'=>$source
                    ]);

                    $elasticsearch->client()->update($bean1)->getBody();

                }
            }

        });

    }


    /**
     * 更改 es 字段  加条件
     */

    private function updateEsFieldWithWhere(){

        go(function (){

            $array = [
                [ "index"=>"equipment", "oldValue"=>1, "newValue"=>0 ],
                [ "index"=>"equipment", "oldValue"=>2, "newValue"=>1 ],

                [ "index"=>"money_change", "oldValue"=>1, "newValue"=>0 ],
                [ "index"=>"money_change", "oldValue"=>2, "newValue"=>1 ],

                [ "index"=>"role_login", "oldValue"=>1, "newValue"=>0 ],
                [ "index"=>"role_login", "oldValue"=>2, "newValue"=>1 ],

                [ "index"=>"soul_synthesize", "oldValue"=>1, "newValue"=>0 ],
                [ "index"=>"soul_synthesize", "oldValue"=>2, "newValue"=>1 ],

                [ "index"=>"change_scene", "oldValue"=>1, "newValue"=>0 ],
                [ "index"=>"change_scene", "oldValue"=>2, "newValue"=>1 ],

                [ "index"=>"equip_identify", "oldValue"=>1, "newValue"=>0 ],
                [ "index"=>"equip_identify", "oldValue"=>2, "newValue"=>1 ],

                [ "index"=>"equip_synthesize", "oldValue"=>1, "newValue"=>0 ],
                [ "index"=>"equip_synthesize", "oldValue"=>2, "newValue"=>1 ],

                [ "index"=>"equip_intensify", "oldValue"=>1, "newValue"=>0 ],
                [ "index"=>"equip_intensify", "oldValue"=>2, "newValue"=>1 ],

                [ "index"=>"equip_synthesize_use_equip_attr", "oldValue"=>1, "newValue"=>0 ],
                [ "index"=>"equip_synthesize_use_equip_attr", "oldValue"=>2, "newValue"=>1 ],

                [ "index"=>"equip_synthesize_use_equip", "oldValue"=>1, "newValue"=>0 ],
                [ "index"=>"equip_synthesize_use_equip", "oldValue"=>2, "newValue"=>1 ],

                [ "index"=>"equip_synthesize_use_material", "oldValue"=>1, "newValue"=>0 ],
                [ "index"=>"equip_synthesize_use_material", "oldValue"=>2, "newValue"=>1 ],

                [ "index"=>"equip_synthesize_new_equip_attr", "oldValue"=>1, "newValue"=>0 ],
                [ "index"=>"equip_synthesize_new_equip_attr", "oldValue"=>2, "newValue"=>1 ],

                [ "index"=>"role_create", "oldValue"=>1, "newValue"=>0 ],
                [ "index"=>"role_create", "oldValue"=>2, "newValue"=>1 ],
            ];
            foreach($array as $item){

                while (true){

                    $bean = new \EasySwoole\ElasticSearch\RequestBean\UpdateByQuery();
                    $bean->setIndex($item['index']);
                    $body = [
                        "script"=>[
                            "source"=>"ctx._source.gameArea = params.newValue",
                            "lang"=> "painless",
                            "params"=>[
                                "newValue"=>$item['newValue']
                            ]
                        ],
                        "query"=>[
                            "bool"=>[
                                "must"=>[
                                    [
                                        "term"=>["gameArea"=>$item['oldValue']]
                                    ]
                                ]
                            ]
                        ]
                    ];
                    $bean->setBody($body);

                    $elasticsearch = app(EsConnector::class)->getInstance();

                    $resBody = $elasticsearch->client()->updateByQuery($bean)->getBody();

                    $resBody = json_decode($resBody,true);

                    $total = data_get($resBody,'total');

                    echo "index: " . $item['index'] . " update total: ". $total ."\n";
                    if ($total == 0) { //此任务更新完毕
                        break;
                    }

                }

            }

        });

    }



}
